package Servlet;

import Service.StudentService.StudentService;
import Service.TeacherService.TeacherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/*
负责教师学生 无密修改密码 数据接口
 */
@WebServlet(value = "/modifyPasswordNoLogin")
public class ModifyPasswordNoLogin extends HttpServlet {
    private StudentService studentService = new StudentService();
    private TeacherService teacherService = new TeacherService();


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String userId = request.getParameter("userId");
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        String status = request.getParameter("status");
;

        int count = 0;
        PrintWriter out = response.getWriter();

        if ("student".equals(status)){
            count = studentService.updatePasswordNoLogin(userId,userName,userPassword);
            request.getSession().invalidate();//清除 session 中的所有信息
        }else if("teacher".equals(status)){
            count = teacherService.updatePasswordNoLogin(userId,userName,userPassword);
            request.getSession().invalidate();//清除 session 中的所有信息
        }

        out.print(count);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
